﻿using System;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace DesignByFarooq.PowershellByExample.Testing
{
    [TestClass]
    public class MemoryStatusFilePartitionUnitTest
    {
        private static readonly int startOffset = 1;
        private static readonly int endOffset = 2;

        private static readonly MemoryStatusFilePartition partition = new MemoryStatusFilePartition(
            startOffset,
            endOffset
            );

        [TestMethod]
        public void CreateInstance()
        {
            int startOffset = 1;

            MemoryStatusFilePartition partition = new MemoryStatusFilePartition(
                startOffset
                );

            Assert.IsNotNull(
                partition
                );

            Assert.AreEqual(
                startOffset,
                partition.StartOffset
                );

            Assert.AreEqual(
                0,
                partition.EndOffset
                );

        }

        [TestMethod]
        public void CreateInstance2()
        {
            int startOffset = 1;
            int endOffset = 100000;

            MemoryStatusFilePartition partition = new MemoryStatusFilePartition(
                startOffset,
                endOffset
                );

            Assert.IsNotNull(
                partition
                );

            Assert.AreEqual(
                startOffset,
                partition.StartOffset
                );

            Assert.AreEqual(
                endOffset,
                partition.EndOffset
                );

        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentOutOfRangeException))]
        public void CreateInstanceWithNegativeStartOffset()
        {
            int startOffset = -1;

            new MemoryStatusFilePartition(
                startOffset
                );
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentOutOfRangeException))]
        public void CreateInstanceWithNegativeEndOffset()
        {
            int startOffset = 1;
            int endOffset = -1;

            new MemoryStatusFilePartition(
                startOffset,
                endOffset
                );
        }

        [TestMethod]
        [ExpectedException(typeof(InvalidOperationException))]
        public void CreateInstanceWithStartOffsetGreaterThanEndOffset()
        {
            int startOffset = 2;
            int endOffset = 1;

            new MemoryStatusFilePartition(
                startOffset,
                endOffset
                );
        }

        [ExpectedException(typeof(InvalidOperationException))]
        public void CreateInstanceWithStartOffsetGreaterThanEndOffsetButEndOffsetIsZero()
        {
            int startOffset = 2;
            int endOffset = 0;

            MemoryStatusFilePartition partition = new MemoryStatusFilePartition(
                startOffset,
                endOffset
                );

            Assert.AreEqual(
                startOffset,
                partition.StartOffset
                );

            Assert.AreEqual(
                endOffset,
                partition.EndOffset
                );
        }

        [TestMethod]
        public void InstancesNotEqual()
        {
            Assert.AreNotEqual(
                MemoryStatusFilePartitionUnitTest.partition,
                null as string
                );
                
        }

        [TestMethod]
        public void InstancesNotEqual2()
        {
            Assert.AreNotEqual(
                MemoryStatusFilePartitionUnitTest.partition,
                3
                );

        }

        [TestMethod]
        public void InstancesEqual()
        {
            MemoryStatusFilePartition copy = MemoryStatusFilePartitionUnitTest.partition;

            Assert.AreEqual(
                MemoryStatusFilePartitionUnitTest.partition,
                copy
                );
        }

        [TestMethod]
        public void InstancesEqual2()
        {
            MemoryStatusFilePartition copy = new MemoryStatusFilePartition(
                MemoryStatusFilePartitionUnitTest.startOffset,
                MemoryStatusFilePartitionUnitTest.endOffset
                );

            Assert.AreEqual(
                MemoryStatusFilePartitionUnitTest.partition,
                copy
                );
        }

        [TestMethod]
        public void InstancesNotEqual3()
        {
            MemoryStatusFilePartition copy = new MemoryStatusFilePartition(
                MemoryStatusFilePartitionUnitTest.startOffset,
                100000
                );

            Assert.AreNotEqual(
                MemoryStatusFilePartitionUnitTest.partition,
                copy
                );
        }

        [TestMethod]
        public void InstancesNotEqual4()
        {
            MemoryStatusFilePartition copy = new MemoryStatusFilePartition(
                0,
                MemoryStatusFilePartitionUnitTest.endOffset
                );

            Assert.AreNotEqual(
                MemoryStatusFilePartitionUnitTest.partition,
                copy
                );
        }

        [TestMethod]
        public void InstancesNotEqual5()
        {
            MemoryStatusFilePartition copy = new MemoryStatusFilePartition(
                100,
                500
                );

            Assert.AreNotEqual(
                MemoryStatusFilePartitionUnitTest.partition,
                copy
                );
        }

    }
}
